﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PeteBrown.ConferenceTrivia.Services;
using System.Collections.ObjectModel;
using PeteBrown.ConferenceTrivia.Model;

namespace PeteBrown.ConferenceTrivia.ViewModels
{
    public class MainViewModel : Observable
    {
        //private TwitterService _twitterService = new TwitterService("@pete_brown", 7, TimeSpan.FromSeconds(30));
        private TwitterService _twitterService = new TwitterService("silverlight", 7, TimeSpan.FromSeconds(30));
        //private TwitterService _twitterService = new TwitterService("pdctrivia", 7, TimeSpan.FromSeconds(30));
        private TriviaClientService _triviaService = new TriviaClientService();


        public ObservableCollection<Tweet> Tweets
        {
            get { return _twitterService.Tweets; }
        }


        private QuestionAndAnswer _currentTriviaItem;
        public QuestionAndAnswer CurrentTriviaItem
        {
            get { return _currentTriviaItem; }
            set { _currentTriviaItem = value; NotifyPropertyChanged("CurrentTriviaItem"); }
        }

        public event EventHandler TriviaLoaded;

        public MainViewModel()
        {
            _triviaService.TriviaLoaded += new EventHandler(OnTriviaLoaded);

            _triviaService.LoadTrivia();
        }

        void OnTriviaLoaded(object sender, EventArgs e)
        {
            MoveFirst();

            if (TriviaLoaded != null)
                TriviaLoaded(this, EventArgs.Empty);
        }


        private bool _triviaLoaded = false;
        public bool IsTriviaLoaded
        {
            get { return _triviaLoaded; }
        }


        int _currentIndex = 0;
        public void MoveFirst()
        {
            if (_triviaService.TriviaItems.Count > 0)
            {
                _currentIndex = 0;
                CurrentTriviaItem = _triviaService.TriviaItems[_currentIndex];
            }
        }

        public void MoveNext()
        {
            if (_triviaService.TriviaItems.Count > 0)
            {
                _currentIndex++;

                // wrap around
                if (_currentIndex > _triviaService.TriviaItems.Count - 1)
                    _currentIndex = 0;

                CurrentTriviaItem = _triviaService.TriviaItems[_currentIndex];
            }
        }

        public void MovePrevious()
        {
            if (_triviaService.TriviaItems.Count > 0)
            {
                _currentIndex--;

                // wrap around
                if (_currentIndex < 0)
                    _currentIndex = _triviaService.TriviaItems.Count - 1;

                CurrentTriviaItem = _triviaService.TriviaItems[_currentIndex];
            }
        }

    }
}
